//
//  MainViewController.swift
//  WeiBoSwift
//
//  Created by rong on 15/6/24.
//  Copyright © 2015年 rong. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

   
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let myTabBar = MainTabBar();
        
        //通过kvc修改只读属性的值
        setValue(myTabBar, forKey: "tabBar")
        
        myTabBar.compoesButton.addTarget(self, action: "composedButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)

        addChildViewControllers();
    }
    
    //发布微博
    func composedButtonClicked()
    {
        let nav = UINavigationController(rootViewController: ComposeViewController())
        presentViewController(nav, animated: true, completion: nil)
        //!!!!! ComposeViewController里面一定要设置背景颜色，不然跳转出现问题
    }
    
    
    //添加子控制器＝＝》改成动态加载类
    private func addChildViewControllers() {
        
        //1.加载json  
        // json文件的数据格式
        //     "vcName": "HomeTableViewController",
        //     "title": "首页",
        //     "imageName": "tabbar_home"
        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)!
        let data = NSData(contentsOfFile: path)!
        
        //2.反序列化  throw 是xcode 7.0 最明显的一个变化
        do{
            // try! 是强行 try， 如果不成功，直接崩溃，后果自负
            let array = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
            
            for dict in array as! [[String:String]]
            {
                addchildviewcontroller(dict["vcName"]!, title: dict["title"]!, imageName: dict["imageName"]!)
            }
            
        } catch  {
        
            //捕获异常
            print(error)
            
            //如果从服务器加载的json出错，直接用字符串加载
            addchildviewcontroller("HomeViewController", title: "首页", imageName: "tabbar_home")
            addchildviewcontroller("MessageViewController", title: "消息", imageName: "tabbar_message_center")
            addchildviewcontroller("DiscoveryViewController", title: "发现", imageName: "tabbar_discover")
            addchildviewcontroller("ProfileViewController", title: "我", imageName: "tabbar_profile")
            
            
        } // (_) 匿名的意思
        
    }
    
    //private func addchildviewcontroller(vc:UIViewController, title: String, imageName:String)
    private func addchildviewcontroller(vcName: String, title: String, imageName:String)
    {
        //设置tintcolor
        tabBar.tintColor = UIColor.orangeColor();
        
        //!!!swift中，类名是包含命名空间的
        //命名空间默认是项目名，同一个命名空间全局共享
        //0. 从info.plist中获取信息
        // 设置对象的类型，在右侧使用  "as! String"   "as? String"
        //print(NSBundle.mainBundle().infoDictionary)
        
        let ns = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        
        //1.将字符串转换成类
        //  告诉编译器，暂时是 AnyClass
        let cls: AnyClass = NSClassFromString(ns + "." + vcName)!
        
        //2.实例化对象，所有的视图控制器最终都继承自  UIViewController
        //对象的准确类型，取决于分配空间指定的类型
        //let vc = cls.alloc() as! UIViewController
        let vc = cls.new() as! UIViewController
        
        //print(vc);
        
        vc.title = title;
        vc.tabBarItem.image = UIImage(named: imageName);
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav);
    }



}
